package cn.eoe.example.sbzx_wljs;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.ShellUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.textView2)
    TextView textView2;
    private String TAG = this.getClass().getName();

    private String ip;
    private String URL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        ButterKnife.bind(this);
        textView2.setText(R.string.msg);
        ip=getResources().getString(R.string.ip);
        URL = getResources().getString(R.string.url);
    }

    @Override
    protected void onStart() {
        super.onStart();
        myStrat(ip);
    }

    private void myStrat(String ip) {
        Log.e(TAG, ip);
        if (ip.isEmpty()) {
            ip = "192.168.1.175";
        }
        String finalIp = ip;

        new Thread(new Runnable() {
            String mesg = null;

            @Override
            public void run() {
                if (isAvailableByPing(finalIp)) {
                    Log.e(TAG, "is ok");
                    mesg = "连接服务器成功";
                    mystartActvity(URL);
                } else {
                    Log.e(TAG, "is faste");
                    mesg = "无法连接到服务器";
                }
                myrunOnUiThreadToast(mesg);
            }
        }).start();
    }

    /**
     * 判断网络是否可用
     * <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
     * <p>需要异步ping，如果ping不通就说明网络不可用</p>
     *
     * @param ip ip地址（自己服务器ip），如果为空，ip为阿里巴巴公共ip
     * @return {@code true}: 可用<br>{@code false}: 不可用
     */
    public boolean isAvailableByPing(String ip) {
        if (ip == null || ip.length() <= 0) {
            ip = "223.5.5.5";// 阿里巴巴公共ip
        }
        ShellUtils.CommandResult result = ShellUtils.execCmd(String.format("ping -c 1 %s", ip), false);
        boolean ret = result.result == 0;
        String resulsMsg = null;
        if (result.errorMsg != null) {
            Log.d("NetworkUtils", "isAvailableByPing() called" + result.errorMsg);
            resulsMsg = result.errorMsg;
        }
        if (result.successMsg != null) {
            Log.d("NetworkUtils", "isAvailableByPing() called" + result.successMsg);
            resulsMsg = result.successMsg;
        }
        myrunOnUiThread(resulsMsg);
        return ret;
    }

    private void myrunOnUiThread(String resuls) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (resuls != null) {
                    //  textView.setText(resuls);
                }
            }
        });
    }

    private void myrunOnUiThreadToast(String mesg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mesg != null) {
                    Toast.makeText(MainActivity.this, mesg, Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private void mystartActvity(String url) {
        Log.e("Activity", "url：" + url);
        Intent intent = new Intent(this, PlayActivity.class);
        intent.putExtra("url", url);
        startActivity(intent);
        this.finish();
    }

    @OnClick(R.id.button)
    public void onViewClicked() {
        myStrat(ip);
    }
}